﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RiskMan.DataClass.Models;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Collections.ObjectModel;
using AvalonDock;
using RiskMan.DataClass.Models.Quik;
using RiskMan.DataClass.Models.RM;
using System.Windows;
using System.Reflection;

namespace RiskMan.Config
{
    public class XMLHistory
    {
        public static String FILENAME_STOPLIST = "\\StopList.xml";
        public static string FILENAME_DEALLIST = "\\DealList.xml";
        public static string FILENAME_STOPLOSSORDER = "\\StopLossList.xml";
        public static string FILENAME_QUIKITEMS = "\\QuikSettings.xml";
        public static string FILENAME_RMITEMS = "\\RmiItems.xml";
        public static string FILENAME_RMINVESTORITEMS = "\\RMInvestorItems.xml";
        public static string FILENAME_MMVBCOMISITEMS = "\\MMVBItems.xml";
        public static string FILENAME_LIMITSTOCK = "\\LimitStock.xml";
        public static string FILENAME_LIMITMONEYANDDEALS = "\\LimitMoneyAndDeals.xml";
        public static string FILENAME_WINDOW = "\\Window.xml";


        public static object GetXmlData(string filename)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\RiskMan";

            if (!Directory.Exists(path))
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
                dir.Create();
            }

            if (filename == FILENAME_WINDOW)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(List<string>));
                    StreamReader file = new StreamReader(path + filename);
                    List<string> result = new List<string>();
                    result = (List<string>)reader.Deserialize(file);
                    return result;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }

            if (filename == FILENAME_RMINVESTORITEMS)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(ObservableCollection<ModuleInvestorModel>));
                    StreamReader file = new StreamReader(path + filename);
                    return (ObservableCollection<ModuleInvestorModel>)reader.Deserialize(file);
                }
                catch (Exception)
                {
                    return new ObservableCollection<ModuleInvestorModel>();
                }
            }

            if (filename == FILENAME_MMVBCOMISITEMS)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(ObservableCollection<MMVBComisClass>));
                    StreamReader file = new StreamReader(path + filename);
                    return (ObservableCollection<MMVBComisClass>)reader.Deserialize(file);
                }
                catch (Exception)
                {
                    return new ObservableCollection<MMVBComisClass>();
                }
            }

            if (filename == FILENAME_RMITEMS)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(List<RmItem>));
                    StreamReader file = new StreamReader(path + filename);
                    List<RmItem> result = new List<RmItem>();
                    result = (List<RmItem>)reader.Deserialize(file);

                    ObservableCollection<RmItem> res = new ObservableCollection<RmItem>();

                    for (int i = 0; i < result.Count; i++)
                    {
                        result[i].isRun = false;
                        result[i].AccountLimeteItem = new AccountLimiteItem();
                        result[i].ActiveDefensePofit = false;
                        
                        res.Add(result[i]);
                    }
                    return res;

                }
                catch (Exception)
                {
                    return new ObservableCollection<RmItem>();
                }
            }

            if (filename == FILENAME_QUIKITEMS)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(QuikItems));
                    StreamReader file = new StreamReader(path + filename);
                    QuikItems result = new QuikItems();
                    result = (QuikItems)reader.Deserialize(file);
                    for (int i = 0; i < result.Count; i++)
                    {
                        result[i].IsConn = false;
                    }
                    return result;
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                    return new QuikItems();
                }
            }
            if (filename == FILENAME_STOPLIST)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(List<StopListModel>));
                    StreamReader file = new StreamReader(path + filename);
                    List<StopListModel> result = new List<StopListModel>();
                    result = (List<StopListModel>)reader.Deserialize(file);

                    ObservableCollection<StopListModel> res = new ObservableCollection<StopListModel>();

                    for (int i = 0; i < result.Count; i++)
                    {
                        result[i].SeccodeList = new List<string>() { cfg.Unknown };
                        result[i].TypeStopList = new StopListModel()._TypeStopList;
                        result[i].TypeRaschList = new StopListModel()._TypeRaschList;
                        result[i].IsStart = false;
                        result[i].Check = false;

                        res.Add(result[i]);
                    }
                    return res;

                }
                catch (Exception)
                {
                    return new ObservableCollection<StopListModel>();
                }
            }

            if (filename == FILENAME_DEALLIST)
            {
                try
                {

                    XmlSerializer reader = new XmlSerializer(typeof(List<DealModel>));
                    StreamReader file = new StreamReader(path + filename);
                    return (List<DealModel>)reader.Deserialize(file);
                }
                catch (Exception)
                {
                    return new List<DealModel>();
                }
            }

            if (filename == FILENAME_STOPLOSSORDER)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(List<StopOrderModel>));
                    StreamReader file = new StreamReader(path + filename);
                    return (List<StopOrderModel>)reader.Deserialize(file);
                }
                catch (Exception)
                {
                    return new List<StopOrderModel>();
                }
            }

            if (filename == FILENAME_LIMITSTOCK)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(ObservableCollection<ModuleLimitStockModel>));
                    StreamReader file = new StreamReader(path + filename);
                    return (ObservableCollection<ModuleLimitStockModel>)reader.Deserialize(file);
                }
                catch (Exception)
                {
                    return new ObservableCollection<ModuleLimitStockModel>();
                }
            }

            if (filename == FILENAME_LIMITMONEYANDDEALS)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(ObservableCollection<LimitMoneyAndDealsModel>));
                    StreamReader file = new StreamReader(path + filename);
                    return (ObservableCollection<LimitMoneyAndDealsModel>)reader.Deserialize(file);
                }
                catch (Exception)
                {
                    return new ObservableCollection<LimitMoneyAndDealsModel>();
                }
            }

            return null;
        }

        public static void SetXmlData(string filename, object List)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\RiskMan";

            if (!Directory.Exists(path))
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
                dir.Create();
            }


            if (filename == FILENAME_WINDOW)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<string>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.Message, new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }


            if (filename == FILENAME_RMINVESTORITEMS)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(ObservableCollection<ModuleInvestorModel>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.Message, new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

            if (filename == FILENAME_MMVBCOMISITEMS)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(ObservableCollection<MMVBComisClass>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }
            if (filename == FILENAME_RMITEMS)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<RmItem>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

            if (filename == FILENAME_QUIKITEMS)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(QuikItems));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

            if (filename == FILENAME_STOPLIST)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<StopListModel>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

            if (filename == FILENAME_DEALLIST)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<DealModel>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, (List<DealModel>)List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

            if (filename == FILENAME_STOPLOSSORDER)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<StopOrderModel>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, ((List<StopOrderModel>)List).Where(a => a.Status == cfg.Active).ToList());
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }


            }
            if (filename == FILENAME_LIMITSTOCK)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<ModuleLimitStockModel>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }

            }

            if (filename == FILENAME_LIMITMONEYANDDEALS)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(ObservableCollection<LimitMoneyAndDealsModel>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.Message, new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }
        }
    }
}